package com.kungfuhacking.wristbandpro.login.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.login.view.IRestpasswordView;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.CommonUtils;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * 重置密码
 * Created by Aaron on 17/4/17.
 */

public class IResetPasswordPresenter implements BasePresenter {

    private IRestpasswordView iRestpasswordView;

    public IResetPasswordPresenter(IRestpasswordView iRestpasswordView) {
        this.iRestpasswordView = iRestpasswordView;
    }

    public void checkInfo() {

        if (TextUtils.isEmpty(iRestpasswordView.getNewPassword())
                || iRestpasswordView.getNewPassword().length() < 6
                || iRestpasswordView.getNewPassword().length() > 16) {
            iRestpasswordView.showErr("密码在6~16位之间");
            return;
        }

        if (!iRestpasswordView.getNewPassword().equals(iRestpasswordView.getNewPasswordRepet())) {
            iRestpasswordView.showErr("两次输入的密码不一致");
            return;
        }

        setNewPwd();

    }

    private void setNewPwd() {

        JSONObject j = new JSONObject();
        j.put("token", iRestpasswordView.getToken());
        j.put("pwd", CommonUtils.toMd5(iRestpasswordView.getNewPassword().getBytes()));
        RetrofitHttpUtil.getInstance().forget(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                iRestpasswordView.hideLoading();
                iRestpasswordView.showErr("更改成功");
                //更新缓存中的密码
                iRestpasswordView.getSp().edit().putString(
                        Constant.PASSWORD, CommonUtils.toMd5(iRestpasswordView.getNewPassword().getBytes())).apply();

                iRestpasswordView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onHandleError(String message) {
                iRestpasswordView.hideLoading();
                iRestpasswordView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iRestpasswordView.showErr(message);
                iRestpasswordView.hideLoading();
                iRestpasswordView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iRestpasswordView.showLoading("正在修改密码...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
